package com.zdkj.subway.stm.util;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.atomic.AtomicInteger;

public class SerialNumberGenerator {
    private final AtomicInteger counter = new AtomicInteger(0);
    private LocalDate currentDate = LocalDate.now();

    public synchronized String generateSerialNumber() {
        LocalDate today = LocalDate.now();

        // 如果日期发生变化，重置计数器
        if (!today.equals(currentDate)) {
            currentDate = today;
            counter.set(0);
        }

        // 获取当前计数值并递增
        int currentCounter = counter.incrementAndGet();

        // 格式化日期
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMMdd");
        String datePart = today.format(formatter);

        // 格式化计数器，确保至少有4位数（例如0001）
        String counterPart = String.format("%04d", currentCounter);

        // 组合成完整的流水号
        return datePart + counterPart;
    }
}
